﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;

namespace DevEx
{
    public class CHopDong
    {
        private static QLVTDataContext GetDc()
        {
            QLVTDataContext dc = new QLVTDataContext();
            return dc;
        }
                
        public IQueryable<tblHopDong> SelectAllRecord()
        {
            QLVTDataContext dc = GetDc();

            var q = from p in dc.tblHopDongs
                    orderby p.SoHopDong 
                    select p;
                    
            return q;
        }

        public tblHopDong SelectRecordByID(int ID_HopDong)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblHopDongs where p.ID_HopDong == ID_HopDong select p).SingleOrDefault();
            return q;
        }

        public tblHopDong SelectRecordByName(String HopDong)
        {
            QLVTDataContext dc = GetDc();

            var q = (from p in dc.tblHopDongs where p.SoHopDong.ToLower()  == HopDong.ToLower() select p).SingleOrDefault();
            return q;
        }

        //Insert
        public void Insert(tblHopDong obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblHopDongs.InsertOnSubmit(obj);
            dc.SubmitChanges();
        }
        //Delete
        public void Delete(int ID)
        {
            QLVTDataContext dc = GetDc();

            var HopDong = dc.tblHopDongs.SingleOrDefault(p=>p.ID_HopDong == ID);
            dc.tblHopDongs.DeleteOnSubmit(HopDong);
            dc.SubmitChanges();
        }
        //Update
        public void Update(tblHopDong obj)
        {
            QLVTDataContext dc = GetDc();
            dc.tblHopDongs.Attach(obj);
            dc.Refresh(RefreshMode.KeepCurrentValues, obj);
            dc.SubmitChanges(ConflictMode.ContinueOnConflict);
        }
    }
}
